﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Customer_OrderOnline : System.Web.UI.Page
{
    CustormerMethord cm = new CustormerMethord();
    DataTable tbGioHang = new DataTable();
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            load_methodPay();
        }
         
        if (Session["Shopping"] == null)
        {
            Response.Redirect("ShoppingHollow.aspx");
        }
         
        int idkh = 3;
        tbGioHang = (DataTable)Session["Shopping"];
 
 
        string strnumber = tbGioHang.Compute("Sum(Total)", "").ToString();
        lblTotal.Text = strnumber;
        gvOrder.DataSource = tbGioHang;
        gvOrder.DataBind();
        txtDate.Text = DateTime.Now.Date.ToString();
    }

private void load_methodPay()
{

    dlCard.DataSource = cm.LoadCard();
    dlCard.DataValueField = "CardTypeId";
    dlCard.DataTextField = "CardType";
    dlCard.DataBind();
}
protected void btnBuy_Click(object sender, EventArgs e)
{
    int idkh = 1;
        if (Session["Shopping"] == null)
        {
            Response.Redirect("ShopHollow.aspx");
        }

        DateTime date = Convert.ToDateTime(txtDate.Text);
        try{
            cm.AddOrder(idkh, date, "In Progress");
        }catch (Exception ex){
        Response.Redirect("Errors.aspx");
        }
 
        
        
        try{
        dt = cm.GetOrderID();
        }catch(Exception ex){
        Response.Redirect("Errors.aspx");
        }
        int OrderID= 0;
        foreach (DataRow row in  dt.Rows)
        {
            OrderID = int.Parse(row[0].ToString());
        }
        if (tbGioHang.Rows.Count > 0)
        {
            foreach (DataRow row in tbGioHang.Rows)
            {
                int idSP = int.Parse(row["ProductID"].ToString());
                int SoLuong = int.Parse(row["Quantity"].ToString());
                float Gia = float.Parse(row["Price"].ToString());
                cm.AddOrderDetails(OrderID,idSP,Gia,SoLuong,txtConsigneeAddress.Text,txtComment.Text);                
            }
        }
        Session["Shopping"] = null;
        Response.Redirect("OrderCompleted.aspx");
    }
}
